################################################################################
## Group Identities and Parliamentary Debates: Replication package
## Fiva, Nedregård and Øien (2025)

# Description:

## Code to make Figure A.1: "Party discipline measured by roll-call votes 1944-2021"
## Panel A and B

## The input data is confidential and not available in the replication package.
## See the README file for how to get access to the data

################################################################################


# Packages

library(data.table)
library(dplyr)
library(ggplot2)

# Directories (wd is set by master.R)

dir           <- "../data/1_raw_data"
fig.dir  <-  "../results/figures"

# Add montserrat font

library(showtext)
font_add_google(name = "Montserrat", family = "Montserrat")
showtext_auto()


# Data

d <- haven::read_dta(paste(dir, "Voteringsarkivet.dta", sep = "/"))

## Data wrangling

d <- d %>% filter(SAL == 1) %>%  # PLENARY
  mutate(date         = substr(id_vot, 1, 10),
         calendaryear = year(date),
         mth          = month(date)) 

d <- d %>%
  mutate(year = ifelse(mth %in% 10:12, calendaryear + 1, calendaryear)) %>%
  filter(year > 1993) # incomplete data before 1989-1990 session. Start with 1994 session to have seven complete election periods


d <- d %>%
  group_by(PERSON, id_vot) %>% 
  filter(n() == 1) %>% #/* keeping only votes where MPs cast only one vote for that bill*/
  ungroup()


d <- d %>%
  group_by(year, PERSON, fornavn, etternavn, PARTI, id_vot) %>%
  summarise(VOTERING = mean(VOTERING, na.rm = T), .groups = "drop")

d <- d %>%
  # Sort the data by the given columns
  arrange(id_vot, PARTI, PERSON) %>%
  # Exclude FOLKVORD, BASTSESEN
  filter(
    !(PARTI %in% c(0, 11, 23, 24, 98, 205, 238, 310))
  ) %>%
  mutate(count_yes = ifelse(VOTERING == 1, 1, 0),
         
         count_no = ifelse(VOTERING == 2, 1, 0)) %>%
  filter(VOTERING != 3) %>% #/* ABSENT , BUT JUST AFTER 2010 */
  group_by(id_vot, PARTI) %>%
  mutate(party_yes = mean(count_yes),
         party_no   = mean(count_no)) %>%
  group_by(PARTI, id_vot, year) %>%
  summarise(party_yes = mean(party_yes), .groups = "drop") %>%
  mutate(party_discipline = case_when(party_yes>0 & party_yes<1 ~ 0, TRUE ~ 1))


# Plotting panel A of figure A1

pdf(paste(fig.dir, "figA1a.pdf", sep = "/"), width = 9, height = 7, pointsize = 12)

party_yes <- d$party_yes
hist.sum <- hist(party_yes, plot = F)
hist.sum$density = hist.sum$counts/sum(hist.sum$counts)*100
# Convert histogram counts to percentages
plot(hist.sum,freq = F, xlab = "Fraction of legislators in each party voting yes", 
     ylab = "Percent", ylim = c(0, 60), main  = "Panel A", cex.main = 25/12, 
     family = "Montserrat", font.main = 1, cex.lab = 18/12, las = 1)

dev.off()

# Plotting panel B of figure A1

d %>%
  group_by(year) %>%
  summarise(party_discipline = mean(party_discipline*100)) %>%
  ggplot(aes(x = year, y = party_discipline)) +
  geom_point(size = 4, color = "black", fill = "white") +
  geom_line() + 
  theme(
    panel.grid = element_blank(),
    panel.border = element_blank(),
    panel.grid.major.y= element_line(linewidth = 0.2, colour = "grey90"),
    panel.background = element_rect(fill = "transparent"),
    plot.background = element_rect(fill = "transparent"),
    plot.title = element_text(family = "Montserrat", color = "black", size = 25, hjust = 0.5, margin=margin(0,0,25,0),),
    panel.grid.major.x = element_blank(),
    strip.text = element_text(family = "Montserrat", color = "black", size = 14),
    strip.background = element_blank(),
    axis.ticks.length = unit(0, "lines"),
    axis.text.y = element_text(family = "Montserrat", size = 11),
    axis.title.y = element_text(family = "Montserrat", size = 18, margin=margin(0,15,0,0)),
    axis.text.x = element_text(family = "Montserrat", size = 11, angle = 45, hjust = 1),
    axis.title.x = element_text(family = "Montserrat", size = 18, margin=margin(10,0,0,0)),
  ) +
  scale_y_continuous(lim = c(0, 100), breaks = seq(0,100,10)) +
  scale_x_continuous(breaks = 1994:2021) +
  labs(x = "Parliamentary session", y = "Percent of votes where party is united", title = "Panel B") 

ggsave(paste(fig.dir, "figA1b.pdf", sep = "/"), width = 9, height = 7, pointsize = 13)












